package com.tusuo.stds.ui.gender;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.ImageView;

import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_set_gender)
public class SetGenderActivity extends BaseToolbarActivity<SetGenderView, SetGenderPresenter>
                                implements SetGenderView {

    public static final int GENDER_MALE = 1;
    public static final int GENDER_FEMALE = 2;

    @ViewById(R.id.set_gender_iv_male)
    ImageView ivMale;
    @ViewById(R.id.set_gender_iv_female)
    ImageView ivFemale;

    @StringRes(R.string.toolbar_title_set_gender)
    String toolbarTitle;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    @AfterViews
    void afterViews() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

        int intGender = getIntent().getIntExtra("gender", 0);

        switch (intGender) {
            case GENDER_MALE:
                updateGenderView(GENDER_MALE);
                break;
            case GENDER_FEMALE:
                updateGenderView(GENDER_FEMALE);
                break;
            default:
                selectMale();
                break;
        }

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public SetGenderPresenter createPresenter() {
        return new SetGenderPresenter(this);
    }

    @Override
    public void updateGenderView(int gender) {
        switch (gender) {
            case GENDER_MALE:
                ivMale.setImageResource(R.drawable.gender_ic_checked);
                ivFemale.setImageResource(R.drawable.gender_ic_unchecked);
                break;
            case GENDER_FEMALE:
                ivMale.setImageResource(R.drawable.gender_ic_unchecked);
                ivFemale.setImageResource(R.drawable.gender_ic_checked);
                break;
            default:
                ivMale.setImageResource(R.drawable.gender_ic_unchecked);
                ivFemale.setImageResource(R.drawable.gender_ic_unchecked);
                break;
        }
    }

    @Override
    public void backToMainPage() {
        ApplicationHelper.getInstance().removeActivity(this);
    }

    @Click(R.id.set_gender_layout_male)
    void selectMale() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.setGender(GENDER_MALE);
    }

    @Click(R.id.set_gender_layout_female)
    void selectFemale() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.setGender(GENDER_FEMALE);
    }
}
